﻿////////////////////////////////////////////////////////////////////////////////
//
// Ariaware Source Code - Confidential Material
// Copyright © 2004 Aral Balkan. All Rights Reserved.
// Copyright © 2004 Ariaware Limited.
//
// Project: ARP Pizza Sample Application
// File: com/ariaware/pizza/control/Controller.as
// Created by: Aral Balkan
//
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Imports
////////////////////////////////////////////////////////////////////////////////

import com.ariaware.arp.ControllerTemplate;	// ControllerTemplate

import com.ariaware.pizza.command.*;		// commands
import com.ariaware.pizza.view.*;			// views

////////////////////////////////////////////////////////////////////////////////
//
//
// Class: Controller
//
//
////////////////////////////////////////////////////////////////////////////////
class com.ariaware.pizza.control.Controller extends com.ariaware.arp.ControllerTemplate
{
	//
	// Properties
	//
	
	private static var inst:Controller; 	// instance of self
	
	// Inherited: app (Application instance)

	////////////////////////////////////////////////////////////////////////////
	//
	// Primitive operations (private)
	//
	////////////////////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////////////////////
	// addEventListeners()
	////////////////////////////////////////////////////////////////////////////
	private function addEventListeners ()
	{
		//
		// Listen for events from the view. Two separate screens may dispatch
		// the same event and these will be handled by the same event handler.
		// No two screens should use the same event for different purposes.
		//
		
		trace ("INFO Controller::addEventListeners()");
		
		app.orderForm.addEventListener ( "orderPizza", this );
		app.viewOrdersForm.addEventListener ( "getOrderList", this );
		app.viewOrdersForm.addEventListener ( "cancelOrder", this );
	}
	
	////////////////////////////////////////////////////////////////////////////
	// addCommands()
	////////////////////////////////////////////////////////////////////////////
	private function addCommands ()
	{
		//
		// Note: Commands are added as references to the classes. Allows a 
		// single command to be called from multiple views.
		//
		trace ("INFO Controller::addCommands()");
		
		addCommand ( "orderPizzaCommand", OrderPizzaCommand );
		addCommand ( "getOrderListCommand", GetOrderListCommand );
		addCommand ( "cancelOrderCommand", CancelOrderCommand );
	}
	
	////////////////////////////////////////////////////////////////////////////
	//
	// Public methods
	//
	////////////////////////////////////////////////////////////////////////////
	
	////////////////////////////////////////////////////////////////////////////
	// getInstance() 
	////////////////////////////////////////////////////////////////////////////
	public static function getInstance ( appRef )
	{
		//
		// Return reference to singleton instance
		//
		if ( inst == null )
		{
			// create a single instance of the singleton
			inst = new Controller();
			if ( appRef != undefined )
			{
				// register the application
				inst.registerApp ( appRef );
			}
			return inst;
		}
		else
		{
			// instance already exists, return a reference to it
			return inst;
		}
	}	

}
